
/*
Copyright 2009 Peter Ekler, Akos Ludanyi, Budapest University of Technology and Economics, Department of Automation and Applied Informatics, Applied Mobile Research Group (AMORG) http://amorg.aut.bme.hu

This file is part of MobTorrent.

MobTorrent is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

MobTorrent is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with MobTorrent.  If not, see <http://www.gnu.org/licenses/>.
*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ui;

import TorrentEngine.Statistics;
import fileselectdialog.MTFileSelectDialogListener;
import data.LocalizationSupport;
import data.MEStatistics;
import data.MTPreferences;
import fileselectdialog.MTFileSelectDialog;
import javax.microedition.lcdui.Choice;
import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import network.MTUpdateManager;

/**
 *
 * @author Tyrael
 */
public class MTFormSettings extends Form implements CommandListener,
    MTFileSelectDialogListener, MTDialogYesNo.MTDialogYesNoListener, MTUpdateManager.MTUpdateManagerListener
{
    public static final int ScreenID = 2;

    private MTUIManager uiManager;
    private Command command_Back;
    private Command command_SelectDownloadFolder;
    private Command command_SearchForUpdates;
    private StringItem stringItemDownloadFolder;
    private ChoiceGroup choiceGroupLanguage;
    private ChoiceGroup choiceGroupAddressBooster;
    private MTUpdateManager updateManager;

    private ChoiceGroup enableStats;

    public MTFormSettings(String aTitle, MTUIManager aUIManager)
    {
        super(aTitle);
        uiManager = aUIManager;
        updateManager = new MTUpdateManager(this);

        stringItemDownloadFolder = new StringItem(LocalizationSupport.getMessage("label_downloadFolder")+":", MTPreferences.DownloadFolder);
        append(stringItemDownloadFolder);

        choiceGroupLanguage = new ChoiceGroup(LocalizationSupport.getMessage("label_language")+":", Choice.EXCLUSIVE);
        choiceGroupLanguage.append(LocalizationSupport.getMessage("label_english"), null);
        choiceGroupLanguage.append(LocalizationSupport.getMessage("label_hungarian"), null);

        switch (MTPreferences.LanguageCode)
        {
            case MTPreferences.ELanguageEn:
                choiceGroupLanguage.setSelectedIndex(0, true);
                break;
            case MTPreferences.ELanguageHU:
                choiceGroupLanguage.setSelectedIndex(1, true);
                break;
            default:
                choiceGroupLanguage.setSelectedIndex(0, true);
                break;
        }

        append(choiceGroupLanguage);


        choiceGroupAddressBooster = new ChoiceGroup(LocalizationSupport.getMessage("label_enableAddressBooster")+":", Choice.EXCLUSIVE);
        choiceGroupAddressBooster.append(LocalizationSupport.getMessage("label_yes"), null);
        choiceGroupAddressBooster.append(LocalizationSupport.getMessage("label_no"), null);
        if (MTPreferences.EnableAddressBooster)
            choiceGroupAddressBooster.setSelectedIndex(0, true);
        else
            choiceGroupAddressBooster.setSelectedIndex(1, true);
        append(choiceGroupAddressBooster);

        enableStats = new ChoiceGroup("", Choice.MULTIPLE, 
            new String [] {LocalizationSupport.getMessage("label_enableStats")}, null);
        enableStats.setSelectedIndex(0, MTPreferences.EnableStats);
        append(enableStats);


        command_Back = new Command(LocalizationSupport.getMessage("command_back"), Command.BACK, 0);
        command_SelectDownloadFolder = new Command(LocalizationSupport.getMessage(
            "command_selectDownloadFolder"), Command.SCREEN, 1);
        command_SearchForUpdates = new Command(LocalizationSupport.getMessage(
            "command_searchForUpdates"), Command.SCREEN, 2);
        addCommand(command_Back);
        addCommand(command_SelectDownloadFolder);
        addCommand(command_SearchForUpdates);
        setCommandListener(this);
    }

    public void commandAction(Command c, Displayable d)
    {
        if (c == command_Back)
        {
            if (choiceGroupAddressBooster.getSelectedIndex() == 0)
                MTPreferences.EnableAddressBooster = true;
            else
                MTPreferences.EnableAddressBooster = false;
            uiManager.synchronizeAddressBooster();
            
            switch (choiceGroupLanguage.getSelectedIndex())
            {
                case 0:
                    if (MTPreferences.LanguageCode != MTPreferences.ELanguageEn)
                    {
                        MTPreferences.LanguageCode = MTPreferences.ELanguageEn;
                        uiManager.show(new MTDialogYesNo(this,
                            LocalizationSupport.getMessage("text_languageneedrestart"),
                            MTDialogYesNo.Task_NeedRestart).getDisplayable());
                    }
                    else
                    {
                        uiManager.showScreen(MTListTorrents.ScreenID);
                    }
                    break;
                case 1:
                    if (MTPreferences.LanguageCode != MTPreferences.ELanguageHU)
                    {
                        MTPreferences.LanguageCode = MTPreferences.ELanguageHU;
                        uiManager.show(new MTDialogYesNo(this,
                            LocalizationSupport.getMessage("text_languageneedrestart"),
                            MTDialogYesNo.Task_NeedRestart).getDisplayable());
                    }
                    else
                    {
                        uiManager.showScreen(MTListTorrents.ScreenID);
                    }
                    break;
                default:
                    if (MTPreferences.LanguageCode != MTPreferences.ELanguageEn)
                    {
                        MTPreferences.LanguageCode = MTPreferences.ELanguageEn;
                        uiManager.show(new MTDialogYesNo(this,
                            LocalizationSupport.getMessage("text_languageneedrestart"),
                            MTDialogYesNo.Task_NeedRestart).getDisplayable());
                    }
                    else
                    {
                        uiManager.showScreen(MTListTorrents.ScreenID);
                    }
                    break;
            }

            if(MTPreferences.EnableStats != enableStats.isSelected(0))
            {
                MTPreferences.EnableStats = enableStats.isSelected(0);
                if(MTPreferences.EnableStats)
                {
                    uiManager.getTorrentManager().setStatistics(new MEStatistics());
                }
                else
                {
                    uiManager.getTorrentManager().setStatistics(null);
                }
            }
        }
        else if (c == command_SelectDownloadFolder)
        {
            MTFileSelectDialog folderSelectDialog = new MTFileSelectDialog(
                Display.getDisplay(uiManager.getMidlet()),this);

            folderSelectDialog.showFileSelectDialog(null,false);
        }
        else if (c == command_SearchForUpdates)
        {
            updateManager.searchForUpdates();
        }
    }
    
    public void fileSelected(String aSelectedFileName, String aSelectedFileNameWithoutPath) {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    public void folderSelected(String aSelectedFolderName, String aSelectedFolderNameWithoutPath) {
        stringItemDownloadFolder.setText(aSelectedFolderName);
        MTPreferences.DownloadFolder = aSelectedFolderName;
        uiManager.show(this);
    }

    public void selectionCancelled() {
        uiManager.show(this);
    }

// Interface methods

    public void yesSelected(int aTaskCode)
    {
        switch (aTaskCode)
        {
            case MTDialogYesNo.Task_NeedRestart:
            {
                uiManager.exit();
                break;
            }
            case MTDialogYesNo.Task_SearchForUpdate:
            {
                uiManager.showScreen(MTFormUpdate.ScreenID);
                break;
            }
        }
    }

    public void noSelected(int aTaskCode)
    {
        switch (aTaskCode)
        {
            case MTDialogYesNo.Task_NeedRestart:
            {
                uiManager.showScreen(MTListTorrents.ScreenID);
                break;
            }
            case MTDialogYesNo.Task_SearchForUpdate:
            {
                uiManager.showScreen(MTFormSettings.ScreenID);
                break;
            }
        }
    }

    public void newUpdatesAvailable()
    {
        uiManager.show(new MTDialogYesNo(this,
            LocalizationSupport.getMessage("text_updateNowQuestion"),
            MTDialogYesNo.Task_SearchForUpdate).getDisplayable());
    }

    public void noNewUpdate()
    {
        uiManager.showMessage(LocalizationSupport.getMessage("text_noNewUpdate"));
    }
}

